#include "TimerTask.h"
#include <cassert>

namespace frame {
namespace imp {

std::ostream & operator << (std::ostream & os, const Timer & timer){
    os << "id:" << timer.getId() << ", task:" <<(void*)timer.getTimerTask() <<  ", timespan(ms):" << timer.getTimeSpan();
    return os;
}

void TimerTasks::insertNoFind(std::shared_ptr<Timer> timer){
    assert(timer != nullptr);
    timerTasks.insert(std::make_pair(timer->getId(), timer));
}

bool TimerTasks::insert(std::shared_ptr<Timer> timer){
    assert(timer != nullptr);
    uint64_t timerId = timer->getId();
    auto iter = timerTasks.find(timerId);
    if(iter == timerTasks.end()){
        timerTasks.insert(std::make_pair(timerId, timer));
        return true;
    }
    return false;
}
std::shared_ptr<Timer> TimerTasks::remove(uint64_t timerId){
    auto iter = timerTasks.find(timerId); 
    if(iter != timerTasks.end()){
        auto ret = iter->second;
        timerTasks.erase(iter);
        return ret;
    }
    return nullptr;
}

std::shared_ptr<Timer> TimerTasks::getTimer(int timerId) const{
    auto iter = timerTasks.find(timerId);
    if(iter != timerTasks.end()){
        return iter->second;
    }
    return nullptr;
}


} //end of namespace imp
} //end of namespace  frame
